

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:gluz/app_session.dart';
import 'package:path_provider/path_provider.dart';

extension StringExtion on String {

  String baseUrlForHD() {
    return app.env.forwarderUrl + this;
  }
  String baseUrlForShop() {
    return app.env.shopUrl + this;
  }
  String baseUrlForExhibition() {
    return app.env.exhibitionUrl + this;
  }

  String webQueryParams() {
    String version = app.packageInfo.version;
    String lang = app.isLangRU() ? 'ru' : 'zh';
    String statusBarHeight = MediaQuery.of(Get.context!).padding.top.toString();
    String statusbottom = MediaQuery.of(Get.context!).padding.bottom.toString();
    String mobile = app.getUser()?.phoneNumber ?? '';
    String userId = app.getUser()?.id ?? '';
    String isApp = '1';
    String token = app.getUser()?.token ?? '';
    return '$this?version=$version&lang=$lang&statusBarHeight=$statusBarHeight&statusbottom=$statusbottom&mobile=$mobile&userId=$userId&app=$isApp&token=$token';
  }

  String jdImgFullUrl() {
    return "http://img13.360buyimg.com/n0/$this";
  }

  String localRuTail() {
    if (app.isLangRU()) {
      return "${this}_ru";
    }
    return this;
  }

  bool isPhoneNumerValid(int countryId) {
    if (length == 0) return false;
    return chekMobile(countryId);
  }

  bool chekMobile(int countryId) {
    switch (countryId) {
      case 0:
        return length == 11;
        break;
      case 1: case 2:
        return length == 10;
        break;
      case 3: case 4: case 5: case 7: case 8:
        return length == 9;
        break;
      case 6:
        return length == 8;
        break;
      default:
        return false;
    }
  }


  // 保存 Base64 字符串到本地文件
  Future<String?> saveToFile(String name) async {
    try {
      // 1. 获取应用文档目录路径
      final dir = await getApplicationDocumentsDirectory();

      // 2. 创建文件对象（路径：网页目录/face.txt）
      final file = File('${dir.path}/$name.txt');

      // 3. 写入 Base64 字符串到文件（直接保存为文本）
      await file.writeAsString(this);

      logger.w('Base64 数据已保存到：${file.path}');

      return file.path;
    } catch (e) {
      logger.e('保存失败：$e');
      return null;
    }
  }
}